package com.zmxstudy.gzu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmxstudy.gzu.domain.TbSku;
import com.zmxstudy.gzu.domain.TbSpu;
import com.zmxstudy.gzu.mapper.TbBrandMapper;
import com.zmxstudy.gzu.mapper.TbCategoryMapper;
import com.zmxstudy.gzu.mapper.TbSkuMapper;
import com.zmxstudy.gzu.mapper.TbSpuMapper;
import com.zmxstudy.gzu.utils.ProductCodeMsg;
import com.zmxstudy.gzu.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "Spu管理")
@RestController
@RequestMapping("/product-service/spus")
public class SpuController {
    @Resource
    TbBrandMapper tbBrandMapper;
    @Resource
    TbSkuMapper tbSkuMapper;
    @Resource
    TbSpuMapper tbSpuMapper;
    @Resource
    TbCategoryMapper tbCategoryMapper;

    @Operation(summary = "删除Spu")
    @DeleteMapping("/{id}")
    public Result deleteSpu(@PathVariable Long id) {
        TbSpu tbSpu = tbSpuMapper.selectById(id);
        if (tbSpu == null) {
            return Result.error(ProductCodeMsg.DELETESpu_error);
        }

        LambdaQueryWrapper<TbSku> laq = new LambdaQueryWrapper<>();
        laq.eq(TbSku::getSpuId, tbSpu.getId());
        List<TbSku> list = tbSkuMapper.selectList(laq);


        if (list.size() > 0) {
            return Result.error(ProductCodeMsg.DELETESpu_error);
        }
        tbSpuMapper.deleteById(id);
        return Result.success();
    }


    @Operation(summary = "添加Spu")
    @PostMapping
    public Result addSpu(@RequestBody TbSpu tbSpu) {
        Long shopId = 1L;
        tbSpu.setShopId(shopId);
        tbSpu.setFreightId(1L);
        tbSpu.setIsMarketable("0");
        tbSpuMapper.insert(tbSpu);
        return Result.success();
    }

    @PutMapping
    @Operation(summary = "修改Spu")
    public Result updateSpu(@RequestBody TbSpu tbSpu) {
        tbSpuMapper.updateById(tbSpu);
        return Result.success();
    }


    @Operation(summary = "根据目录id查询Spu")
    @GetMapping("/findByCate/{id}")
    Result<List<TbSpu>> findByCate(@PathVariable Long id) {
        LambdaQueryWrapper<TbSpu> laq = new LambdaQueryWrapper<>();
        laq.eq(TbSpu::getCategory1Id,id).or().eq(TbSpu::getCategory2Id,id).or().eq(TbSpu::getCategory3Id,id);
        List<TbSpu> spuList = tbSpuMapper.selectList(laq);

        return Result.success("查询成功", spuList);
    }


    @Operation(summary = "根据ID查询Spu")
    @GetMapping("/{id}")
    Result<TbSpu> findSpuById(@PathVariable Long id) {
        TbSpu tbSpu = tbSpuMapper.selectById(id);
        if (tbSpu == null) {
            return Result.error(ProductCodeMsg.ADD_error);
        }
        return Result.success("查询成功", tbSpu);
    }

    @Operation(summary = "查询所有Spu")
    @GetMapping
    Result<List<TbSpu>> findAllSpu() {
        List<TbSpu> list = tbSpuMapper.selectList(null);
        return Result.success("查询成功", list);
    }

    @Operation(summary = "模糊匹配Spu")
    @PostMapping("/vague_findSpu")
    Result<List<TbSpu>> vague_findSpu(@Param(value = "str") String str) {
        LambdaQueryWrapper<TbSpu> lqw = new LambdaQueryWrapper<>();
        System.out.println(str);

        lqw.like(TbSpu::getName, str).or().like(TbSpu::getCaption, str).eq(TbSpu::getIsMarketable, "1");
        //SELECT id,name,password,age,tel FROM user WHERE (name LIKE ?)
        List<TbSpu> list = tbSpuMapper.selectList(lqw);
        return Result.success("查询成功", list);
    }


}
